package genealogy.deprecated.datamodel.pedigree.sort;

import genealogy.deprecated.datamodel.pedigree.Date;
import genealogy.deprecated.datamodel.pedigree.LifeEvent;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
 * @author Steve
 * @invariant
 */
@Deprecated
public class ChronologicalEventComparator implements Comparator<LifeEvent> {

	private ChonologicalDateComparator dateComparator;
	private EventNameComparator eventComparator;
	
	public ChronologicalEventComparator(){
		dateComparator = new ChonologicalDateComparator();
		eventComparator = new EventNameComparator();
	}
	
	public int compare(LifeEvent eventA, LifeEvent eventB) {
		assert(eventA != null && eventB != null);
		Date dateA = eventA.getDate();
		Date dateB = eventB.getDate();
		
		if(dateA == null || dateB == null){
			return eventComparator.compare(eventA.getEventName(), eventB.getEventName());
		}
		return dateComparator.compare(dateA, dateB);
	}

	
	private static class EventNameComparator implements Comparator<String>{	
		public static List<String> orderBeforeMap;
		{
			orderBeforeMap = new ArrayList<String>();
			orderBeforeMap.add("BIRTH");
			orderBeforeMap.add("CHRISTENING");
			orderBeforeMap.add("BAPTISM");
			orderBeforeMap.add("MARRIAGE");
			orderBeforeMap.add("SEALING");
		}
		
		public static List<String> orderAfterMap;
		{
			orderBeforeMap = new ArrayList<String>();
			orderBeforeMap.add("DEATH");
			orderBeforeMap.add("BURRIAL");
		}
		
		
		/*
		 * (non-Javadoc)
		 * 
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(String eventNameA, String eventNameB) {
			eventNameA = eventNameA.toUpperCase();
			eventNameB = eventNameB.toUpperCase();

			if(eventNameA.equalsIgnoreCase(eventNameB)){
				return 0;
			}
			
			for(String eventString : orderBeforeMap){
				if(eventNameA.contains(eventString)){
					return -1;
				}
				else if(eventNameB.contains(eventString)){
					return 1;
				}
			}
			
			for(int i = orderAfterMap.size() - 1; i >= 0; i--){
				String eventString = orderAfterMap.get(i);
				if(eventNameA.contains(eventString)){
					return 1;
				}
				else if(eventNameB.contains(eventString)){
					return -1;
				}
			}
			return eventNameA.compareTo(eventNameB);
		}	
	}
}
